package com.telecom.weixi.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.telecom.weixi.R;

public class TitleView extends RelativeLayout {
	TextView title;
	TextView subTitle;
	Button preButton;
	Button postButton;
	
	boolean showPreBtn;
	boolean showPostBtn;
	boolean showTitle;
	boolean showSubTitle;
	String txtTitle;
	String txtSubTitle;
	
	public TitleView(Context context){
		this(context, null);
	}
	public TitleView(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		if(attrs != null){
			TypedArray typeArrays = context.obtainStyledAttributes(attrs, R.styleable.TitleView);
			showPreBtn = typeArrays.getBoolean(R.styleable.TitleView_showPreButton, true);
			showPostBtn = typeArrays.getBoolean(R.styleable.TitleView_showPostButton, false);
			showTitle = typeArrays.getBoolean(R.styleable.TitleView_showTitle, true);
			showSubTitle = typeArrays.getBoolean(R.styleable.TitleView_showSubTitle, false);
			txtTitle = typeArrays.getString(R.styleable.TitleView_title);
			txtSubTitle = typeArrays.getString(R.styleable.TitleView_subTitle);
			
			typeArrays.recycle();
		}else{
			showPreBtn = true;
			showPostBtn = false;
			showTitle = true;
			showSubTitle = false;
			txtTitle = "";
			txtSubTitle = "";
		}
		
		setupView(context);
	}
	
	public void setTitle(String title){
		this.title.setText(title);
	}
	public void setSubTitle(String subTitle){
		this.subTitle.setText(subTitle);
	}
	public void setPreButtonBg(Drawable bg){
		preButton.setBackgroundDrawable(bg);
	}
	public void setPostButtonBg(Drawable bg){
		postButton.setBackgroundDrawable(bg);
	}
	public void setPreButtonListener(View.OnClickListener l){
		preButton.setOnClickListener(l);
	}
	public void setPostButtonListener(View.OnClickListener l){
		postButton.setOnClickListener(l);
	}
	private void setupView(Context context){
		LayoutInflater flater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		flater.inflate(R.layout.title_layout, this);
		title = (TextView) findViewById(R.id.main_title);
		subTitle = (TextView) findViewById(R.id.sub_title);
		preButton = (Button) findViewById(R.id.title_btn_back);
		postButton = (Button) findViewById(R.id.title_btn_action);
		
		title.setText(txtTitle);
		subTitle.setText(txtSubTitle);
		if(!showPreBtn)
			preButton.setVisibility(View.GONE);
		else
			preButton.setVisibility(View.VISIBLE);
		if(!showPostBtn)
			postButton.setVisibility(View.GONE);
		else
			postButton.setVisibility(View.VISIBLE);
		if(showTitle || txtTitle.length() != 0)
			title.setVisibility(View.VISIBLE);
		else
			title.setVisibility(View.GONE);
		if(showSubTitle || subTitle.length() != 0)
			subTitle.setVisibility(View.VISIBLE);
		else
			subTitle.setVisibility(View.GONE);
		
	}
	

}
